package com.example.administrator.x3picturetransliteration.Home.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.x3picturetransliteration.Base.BaseActivity;
import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Constants;
import com.example.administrator.x3picturetransliteration.Home.Presenter.Activity.RegisterPresenter;
import com.example.administrator.x3picturetransliteration.R;
import com.example.administrator.x3picturetransliteration.Util.RegularUtil;
import com.example.administrator.x3picturetransliteration.Util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册
 * Created by Administrator on 2019/8/16.
 */

public class RegisterActivity extends BaseActivity{

    private RegisterPresenter mRegisterPresenter;

    @BindView(R.id.TelephoneEdit)EditText TelephoneEdit; //手机号
    @BindView(R.id.VerificationCodeEdit)EditText VerificationCodeEdit; //验证码
    @BindView(R.id.PwdEdit)EditText PwdEdit; //获取密码

    @BindView(R.id.VerificationCodeText)TextView VerificationCodeText; //获得验证码

    @BindView(R.id.AImg)ImageView AImg; //用户协议按钮

    @BindView(R.id.gsText)TextView gsText;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_register;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String gsName = Constants.getConstants().getmAppDataBean().getCompanyName();

        if (gsName!=null)gsText.setText(gsName);

        mRegisterPresenter = new RegisterPresenter(this,mCompositeSubscriptions);
    }

    /** 返回登录 */
    @OnClick(R.id.RegisterView)
    public void RegisterViewClick(){

        finish();
    }

    /** 同意用户协议 */
    private boolean AJudge = false;
    @OnClick(R.id.A)
    public void AClick(){

        if (AJudge){

            AImg.setImageResource(R.mipmap.home_check0);

        }else {

            AImg.setImageResource(R.mipmap.home_check1);
        }

        AJudge = !AJudge;
    }

    /** 用户协议 */
    @OnClick(R.id.B)
    public void BClick(){

        mRegisterPresenter.Agreement("1",new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                AgreementActivity.startAgreementActivity(RegisterActivity.this,"用户协议",data + "");
            }
        });
    }

    /** 隐私政策 */
    @OnClick(R.id.C)
    public void CClick(){

        mRegisterPresenter.Agreement("2",new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                AgreementActivity.startAgreementActivity(RegisterActivity.this,"隐私政策",data + "");
            }
        });
    }

    /** 获取验证码 */
    @OnClick(R.id.VerificationCodeText)
    public void VerificationCodeTextClick(){


        if (CodeJudge){

            if ("".equals(TelephoneEdit.getText() + "")){

                new ToastUtil(this).getToast(false,"请输入手机号").show();
                return;
            }

            Map<String,String> map = new HashMap<>();

            map.put("mobile",TelephoneEdit.getText() + "");

            mRegisterPresenter.sendRegisterSmsCode(map, new BasePresenter.Callback() {
                @Override
                public void getData(Object data) {

                    StyleCodeText();
                }
            });
        }

    }

    /** 注册 */
    @OnClick(R.id.RegisterText)
    public void RegisterTextClick(){


        if ("".equals(TelephoneEdit.getText() + "")){

            new ToastUtil(this).getToast(false,"请输入手机号").show();
            return;

        }else if("".equals(VerificationCodeEdit.getText() + "")){

            new ToastUtil(this).getToast(false,"请输入验证码").show();
            return;

        }else if("".equals(PwdEdit.getText() + "")){

            new ToastUtil(this).getToast(false,"请输入密码").show();
            return;

        }else if (!AJudge){

            new ToastUtil(this).getToast(false,"请同意用户协议").show();
            return;
        }

        if (RegularUtil.isENG_NUM(PwdEdit.getText() + "") && (PwdEdit.getText() + "").length()>=8 && (PwdEdit.getText() + "").length()<=12){

            Map<String,String> map = new HashMap<>();

            map.put("mobile",TelephoneEdit.getText() + "");
            map.put("code",VerificationCodeEdit.getText() + "");
            map.put("password",PwdEdit.getText() + "");
            //map.put("channelCode", Constants.getConstants().getChannelCode());

            mRegisterPresenter.register(map);

        }else {

            new ToastUtil(this).getToast(false,"密码格式错误").show();
        }

    }


    /** 点击获取验证码时候获取更新文本状态 */
    private volatile boolean CodeJudge = true;
    private void StyleCodeText(){

        CodeJudge = false;

        final Handler handler = new Handler();

        new Thread(new Runnable() {
            @Override
            public void run() {

                for (int i=60;i>0;i--){

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    final int finalI = i;

                    try{

                        if(VerificationCodeText==null)break;

                        handler.post(new Runnable() {
                            @Override
                            public void run() {

                                if(VerificationCodeText==null)return;

                                if (finalI ==1){

                                    VerificationCodeText.setTextColor(getResources().getColor(R.color.HomeActivityForgetTextColor1));
                                    VerificationCodeText.setText("获取验证码");
                                    CodeJudge = true;
                                }else {

                                    VerificationCodeText.setTextColor(getResources().getColor(R.color.HomeActivityForgetTextColor3));
                                    VerificationCodeText.setText(finalI + "s");
                                }
                            }
                        });

                    }catch (Exception e){

                    }
                }

            }
        }).start();
    }

}
